package com.sdhs.projectschedule.controller;

import com.sdhs.common.annotation.Log;
import com.sdhs.common.core.controller.BaseController;
import com.sdhs.common.core.domain.AjaxResult;
import com.sdhs.common.core.page.TableDataInfo;
import com.sdhs.common.enums.BusinessType;
import com.sdhs.projectschedule.domain.GcProgressIntegratedConstruction;
import com.sdhs.projectschedule.domain.GcProgressSupply;
import com.sdhs.projectschedule.domain.gcProgressIntegratedConstructionFill;
import com.sdhs.projectschedule.service.IGcProgressIntegratedConstructionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.util.*;


/**
 * 集成施工进度Controller
 *
 * @author ww
 */
@RestController
@RequestMapping("/progress/construction")
public class GcProgressIntegratedConstructionController extends BaseController {
    @Autowired
    private IGcProgressIntegratedConstructionService gcProgressIntegratedConstructionService;

    /**
     * 查询集成施工进度列表
     */
    @PreAuthorize("@ss.hasPermi('progress:construction:list')")
    @GetMapping("/list")
    public TableDataInfo list(GcProgressIntegratedConstruction gcProgressIntegratedConstruction) {
        List<GcProgressIntegratedConstruction> list = gcProgressIntegratedConstructionService.selectGcProgressIntegratedConstructionList(gcProgressIntegratedConstruction);
        return getDataTable(list);
    }
    @GetMapping("/fillList")
    public TableDataInfo fillList(gcProgressIntegratedConstructionFill gcIntegratedConstructionFill) {
         List<gcProgressIntegratedConstructionFill> list = gcProgressIntegratedConstructionService.selectgcProgressIntegratedConstructionFillList(gcIntegratedConstructionFill);
        return getDataTable(list);
    }
    /**
     * 查询集成施工进度列表
     */
    @PreAuthorize("@ss.hasPermi('progress:construction:totalList')")
    @GetMapping("/totalList")
    public Map<String, Double> totalList(GcProgressIntegratedConstruction gcProgressIntegratedConstruction) {
        List<GcProgressIntegratedConstruction> list =
                gcProgressIntegratedConstructionService.selectGcProgressIntegratedConstructionList(gcProgressIntegratedConstruction);

/*        //根据最新的填报时间获取最新的施工进度
        Optional<GcProgressIntegratedConstruction> latestItem = list.stream()
                .filter(item -> item.getFillingTime() != null)
                .max(Comparator.comparing(GcProgressIntegratedConstruction::getFillingTime));

        double totalConstruction = 0;
        if (latestItem.isPresent()) {
            try {
                String schedule = latestItem.get().getConstructionSchedule();
                if (schedule != null) {
                    totalConstruction = Double.parseDouble(schedule);
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }*/
        // 获取总数
        double totalConstruction = 0;
        for (GcProgressIntegratedConstruction item : list) {
            try {
                if (item.getConstructionSchedule() != null) {
                    totalConstruction += Double.parseDouble(item.getConstructionSchedule());
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }

        // 使用100减去总项目进度
        totalConstruction = Math.min(totalConstruction, 100);
        double remainingProgress = 100 - totalConstruction;

        // 保留两位小数
        DecimalFormat df = new DecimalFormat("#.##");
        totalConstruction = Double.parseDouble(df.format(totalConstruction));
        remainingProgress = Double.parseDouble(df.format(remainingProgress));

        Map<String, Double> result = new HashMap<>();
        result.put("totalConstruction", totalConstruction);
        result.put("remainingConstruction", remainingProgress);
        return result;
    }

    /**
     * 获取集成施工进度详细信息
     */
    @PreAuthorize("@ss.hasPermi('progress:construction:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(gcProgressIntegratedConstructionService.selectGcProgressIntegratedConstructionById(id));
    }

    /**
     * 新增集成施工进度
     */
    @PreAuthorize("@ss.hasPermi('progress:construction:add')")
    @Log(title = "集成施工进度", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GcProgressIntegratedConstruction gcProgressIntegratedConstruction) {
        return toAjax(gcProgressIntegratedConstructionService.insertGcProgressIntegratedConstruction(gcProgressIntegratedConstruction));
    }

    /**
     * 修改集成施工进度
     */
    @PreAuthorize("@ss.hasPermi('progress:construction:edit')")
    @Log(title = "集成施工进度", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GcProgressIntegratedConstruction gcProgressIntegratedConstruction) {
        return toAjax(gcProgressIntegratedConstructionService.updateGcProgressIntegratedConstruction(gcProgressIntegratedConstruction));
    }

    /**
     * 删除集成施工进度
     */
    @PreAuthorize("@ss.hasPermi('progress:construction:remove')")
    @Log(title = "集成施工进度", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(gcProgressIntegratedConstructionService.deleteGcProgressIntegratedConstructionByIds(ids));
    }
}
